﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Using the validation context</title>
  <keywords>
    <keyword translate="true">Validation</keyword>
    <keyword translate="true">ValidationContext</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Using the validation context</text></para>
    </header>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_ViewModelBase" styleclass="Notes" translate="true">ViewModelBase</link><text styleclass="Notes" translate="true"> derives from </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Data_ModelBase" styleclass="Normal" style="font-style:italic;" translate="true">ModelBase</link><text styleclass="Notes" translate="true">, thus all information here also applies to the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_MVVM_ViewModelBase" styleclass="Notes" translate="true">ViewModelBase</link></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Sometimes detailed information about validation is required. This is possible in Catel thanks to the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Data_ValidationContext" styleclass="Normal" translate="true">ValidationContext</link><text styleclass="Normal" translate="true"> class. The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Data_ValidationContext" styleclass="Normal" translate="true">ValidationContext</link><text styleclass="Normal" translate="true"> serves as the container for all validation results that are gathered via the available validation methods. The </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Data_ValidationContext" styleclass="Normal" translate="true">ValidationContext</link><text styleclass="Normal" translate="true"> has lots of methods that all return lists of either </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Data_IFieldValidationResult" styleclass="Normal" translate="true">IFieldValidationResult</link><text styleclass="Normal" translate="true"> or </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Data_IBusinessRuleValidationResult" styleclass="Normal" translate="true">IBusinessRuleValidationResult</link><text styleclass="Normal" translate="true">.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The examples below are shown a starter examples, but you can gather every type of validation result by using the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Data_ValidationContext" styleclass="Normal" translate="true">ValidationContext</link><text styleclass="Normal" translate="true">. To retrieve the validation context of an object, use the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">viewModel</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">new</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">MyViewModel();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">validationContext</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">viewModel.ValidationContext;</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Getting the number of or warnings and errors</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To retrieve the total number of warnings and errors, use the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">int</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">count</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">validationContext.GetValidationCount();</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Getting all the field errors</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To retrieve all the field errors, use the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">fieldErrors</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">validationContext.GetFieldErrors();</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Getting all the field errors of a specific property</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To retrieve all the field errors of a specific property, use the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">fieldErrors</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">validationContext.GetFieldErrors(&quot;MyProperty&quot;);</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Getting all the business rule warnings</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To retrieve all the business rule warnings:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">businessRuleWarnings</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">validationContext.GetBusinessRuleWarnings();</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Getting all the business rule errors with a specific tag</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">To retrieve all the business rule errors with a specific tag, use the following code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">businessRuleErrors</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">validationContext.GetBusinessRuleErrors(&quot;myTag&quot;);</text></para>
  </body>
</topic>
